package com.banmao.CarlCamp.Day07;

/**
 * https://leetcode.cn/problems/reverse-string-ii/
 */
public class LC541 {

    public String reverseStr(String s, int k) {
        char[] chars = s.toCharArray();

        int left = 0, right = 0;
        while (right < chars.length) {
            right++;
            if (right - left == 2 * k) {
                reverse(chars, left, left + k - 1);
                left = right;
            }
        }
        reverse(chars, left, Math.min(left + k - 1, chars.length - 1));
        return String.valueOf(chars);
    }

    private void reverse(char[] chars, int left, int right) {
        while (left < right) {
            char temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;
            left++;
            right--;
        }
    }

    public static void main(String[] args) {
        String s = "abcdefghijklmnopqrst";
        LC541 lc541 = new LC541();
        String str = lc541.reverseStr(s, 2);
        System.out.println(str);

    }

}
